<script setup lang="ts">
//接收外部传递进来的状态
interface Props {
  //库存量
  max: number;
  count: number;
  lable: string;
}
// 接收外部传递进来的状态
const porps = defineProps<Props>();
// 声明当前组件要触发的自定义事件
const emit = defineEmits(["update:count"]);
// 实现[数量]的双向数据绑定
const count = useVModel(porps, "count", emit);
//用户点击改变数量
function Onchange(setp: number) {
  const nextCount = porps.count + setp;
  if (nextCount < 1) {
    count.value = 1;
  } else if (nextCount > porps.max) {
    count.value = porps.max;
  } else {
    count.value = nextCount;
  }
}
</script>
<template>
  <div class="xtx-number-box">
    <div class="label">{{ porps.lable }}</div>
    <div class="number-box">
      <a href="javascript:" @click="Onchange(-1)">-</a>
      <input type="text" readonly :value="count" />
      <a href="javascript:" @click="Onchange(1)">+</a>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-number-box {
  display: flex;
  align-items: center;
  .label {
    width: 60px;
    color: #999;
    padding-left: 10px;
  }
  .number-box {
    width: 120px;
    height: 30px;
    border: 1px solid #e4e4e4;
    display: flex;
    > a {
      width: 29px;
      line-height: 28px;
      text-align: center;
      background: #f8f8f8;
      font-size: 16px;
      color: #666;
      &:first-of-type {
        border-right: 1px solid #e4e4e4;
      }
      &:last-of-type {
        border-left: 1px solid #e4e4e4;
      }
    }
    > input {
      width: 60px;
      padding: 0 5px;
      text-align: center;
      color: #666;
    }
  }
}
</style>
